package com.graduation.mathematics.ui.fragment.student;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.graduation.mathematics.R;
import com.graduation.mathematics.adapter.BaseExpandableAdapter;
import com.graduation.mathematics.adapter.FunnyExamAdapter;
import com.graduation.mathematics.adapter.HistoryAdapter;
import com.graduation.mathematics.adapter.QuickItemDecoration;
import com.graduation.mathematics.base.BaseFragment;
import com.graduation.mathematics.bean.FunnyExam;
import com.graduation.mathematics.ui.activity.FunnyExamDetailActivity;
import com.graduation.mathematics.ui.widget.CustomWeekBar;
import com.graduation.mathematics.util.UIHelper;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarLayout;
import com.haibin.calendarview.CalendarView;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @ProjectName: mathematicsWorkbook
 * @Package: com.graduation.mathematics.ui.fragment.student
 * @ClassName: HistoryFragment
 * @Description: java类作用描述
 * @Author: APC
 * @CreateDate: 2021/2/23 15:59
 * @Version: 1.0
 */
public class HistoryFragment extends BaseFragment {
    @BindView(R.id.tv_month_day)
    TextView mTextMonthDay;
    @BindView(R.id.tv_year)
    TextView mTextYear;
    @BindView(R.id.tv_lunar)
    TextView mTextLunar;
    @BindView(R.id.tv_current_day)
    TextView mTextCurrentDay;
    @BindView(R.id.calendarView)
    CalendarView mCalendarView;
    @BindView(R.id.calendarLayout)
    CalendarLayout mCalendarLayout;
    @BindView(R.id.recycler_funntexam)
    RecyclerView recyclerView;
    private int mYear;
    private HistoryAdapter historyAdapter;
    List<BaseExpandableAdapter.Unit<String, FunnyExam>> dirs = new ArrayList<>();
    protected List<FunnyExam> mFunnyExamList = new ArrayList<>(); // 所有类型故障集合


    public String[] groupStrings;
    public String[][] childStrings;

    public static HistoryFragment newInstance(){
        return new HistoryFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_history;
    }

    @Override
    protected void initEventAndData() {

        initView();

        initData();
    }

    private void initView(){
        Context context = getActivity();
        if (context == null) {
            return;
        }
        mYear = mCalendarView.getCurYear();
        mTextYear.setText(String.valueOf(mYear));
        mTextMonthDay.setText(UIHelper.getString(R.string.date_month_day, mCalendarView.getCurMonth(), mCalendarView.getCurDay()));
        mTextLunar.setText("今日");
        mTextCurrentDay.setText(String.valueOf(mCalendarView.getCurDay()));



        LinearLayoutManager manager  = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(manager);
        recyclerView.addItemDecoration(new QuickItemDecoration(getActivity(), QuickItemDecoration.VERTICAL_LIST));

        historyAdapter = new HistoryAdapter(context, R.layout.layout_expandalbe_group_item, R.layout.layout_expandalbe_child_item, dirs);
        recyclerView.setAdapter(historyAdapter);
        historyAdapter.setOnItemClickListener((view, position, parent, funnyExam) -> {
            Object o = historyAdapter.getItem(position);
            ToastUtils.showLong("点击了:" + parent + "-" + funnyExam.getQuestionId());

            Bundle bundle = new Bundle();
            bundle.putSerializable(FunnyExamDetailActivity.KEY, funnyExam);
            Intent intent = new Intent();
            intent.putExtra(FunnyExamDetailActivity.BUNDLE_KEY, bundle);
            intent.setClass(getActivity(), FunnyExamDetailActivity.class);
            startActivity(intent);
        });

        mCalendarView.setWeekStarWithSun();
        mCalendarView.setWeekBar(CustomWeekBar.class);

        initListener();

    }

    private Calendar getSchemeCalendar(int year, int month, int day, int color, String text) {
        Calendar calendar = new Calendar();
        calendar.setYear(year);
        calendar.setMonth(month);
        calendar.setDay(day);
        calendar.setSchemeColor(color);//如果单独标记颜色、则会使用这个颜色
        calendar.setScheme(text);
        calendar.addScheme(new Calendar.Scheme());
        return calendar;
    }

    private void initListener() {
        mCalendarView.setOnCalendarSelectListener(new CalendarView.OnCalendarSelectListener() {
            @Override
            public void onCalendarOutOfRange(Calendar calendar) {

            }

            @Override
            public void onCalendarSelect(Calendar calendar, boolean isClick) {
                mTextLunar.setVisibility(View.VISIBLE);
                mTextYear.setVisibility(View.VISIBLE);
                mTextMonthDay.setText(UIHelper.getString(R.string.date_month_day, calendar.getMonth(), calendar.getDay()));
                mTextYear.setText(String.valueOf(calendar.getYear()));
                mTextLunar.setText(calendar.getLunar());
                mYear = calendar.getYear();

//                getSports(DateUtils.formatStringDateShort(calendar.getYear(), calendar.getMonth(), calendar.getDay()));

                LogUtils.d("onDateSelected", "  -- " + calendar.getYear() +
                        "  --  " + calendar.getMonth() +
                        "  -- " + calendar.getDay() +
                        "  --  " + isClick + "  --   " + calendar.getScheme());
            }
        });
        mCalendarView.setOnYearChangeListener(year -> {
            mTextMonthDay.setText(String.valueOf(year));
        });
    }

    @OnClick({R.id.fl_current, R.id.tv_month_day})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.fl_current:
                mCalendarView.scrollToCurrent();
                break;
            case R.id.tv_month_day:
                if (!mCalendarLayout.isExpand()) {
                    mCalendarLayout.expand();
                    return;
                }
                mCalendarView.showYearSelectLayout(mYear);
                mTextLunar.setVisibility(View.GONE);
                mTextYear.setVisibility(View.GONE);
                mTextMonthDay.setText(String.valueOf(mYear));
                break;
//            case R.id.reBack:
//                showTipDialog("退出登录",
//                        "退出登录后将会删除历史数据,\n下次登录依然可以使用本账号!",
//                        () -> logOut());
//                break;
            default:
                break;
        }
    }

    private void initData(){
        groupStrings = getActivity().getResources().getStringArray(R.array.funnyexamList);
        mFunnyExamList.clear();
        dirs.clear();
        mFunnyExamList = LitePal.findAll(FunnyExam.class);
        for (int i = 0;i<groupStrings.length; i ++) {
            BaseExpandableAdapter.Unit<String, FunnyExam> unit = new BaseExpandableAdapter.Unit<>(groupStrings[i], mFunnyExamList);
            dirs.add(unit);
        }

        historyAdapter.setItemList(dirs);
    }
}
